package com.woniuxy.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.coupon.client.CouponManageClient;
import com.woniuxy.coupon.client.CouponQueryClient;
import com.woniuxy.coupon.client.result.CouponDto;
import com.woniuxy.member.common.MemberResultCode;
import com.woniuxy.member.infra.mapper.MemberMapper;
import com.woniuxy.member.infra.model.*;
import com.woniuxy.member.service.*;
import com.woniuxy.member.service.dto.MemberDto;
import com.woniuxy.table.client.TableUserRecordMannageClient;
import com.woniuxy.table.client.param.AddTableUsedRecordParam;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.Date;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ExchangeFoodService exchangeFoodService;
    @Autowired
    private ExchangeCouponService exchangeCouponService;
    @Autowired
    private MemberCouponService memberCouponService;
    @Autowired
    private TableUserRecordMannageClient tableUserRecordMannageClient;
    @Autowired
    private ExchangeRecordService exchangeRecordService;
    @Autowired
    private CouponQueryClient couponQueryClient;
    @Autowired
    private CouponManageClient couponManageClient;
    @Override
    public MemberDto addAndLoginMember(String wechatNum, String openId, String phone, String userName, Integer storeId,String memberImg) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wechat_num",wechatNum).eq("store_id",storeId);
        Member member = memberMapper.selectOne(queryWrapper);
        if (member!=null){
            return BeanUtil.copyProperties(member,MemberDto.class);
        }
        QueryWrapper<Member> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("phone",phone).eq("store_id",storeId);
        Member member1 = memberMapper.selectOne(queryWrapper1);
        if (member1!=null){
            throw new MallException(MemberResultCode.DUPLICATE_PHONE_NUMBER);
        }
        Member addMember = new Member();
        addMember.setStoreId(storeId);
        addMember.setRoleId(1);
        addMember.setWechatNum(wechatNum);
        addMember.setPhone(phone);
        addMember.setUserName(userName);
        addMember.setMemberImg(memberImg);
        addMember.setOpenId(openId);
        save(addMember);
        QueryWrapper<Member> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wechat_num",wechatNum).eq("store_id",storeId);
        Member member2 = memberMapper.selectOne(queryWrapper2);
        return BeanUtil.copyProperties(member2,MemberDto.class);
    }

    @Override
    public void exchangeFood(Integer memberId, Integer exchangeFoodId,Integer tableId) {
        Member member = getById(memberId);
        if (member==null){
            throw new MallException(MemberResultCode.USER_DOES_NOT_EXIST);
        }
        ExchangeFood exchangeFood = exchangeFoodService.getById(exchangeFoodId);
        if (member.getScore()<exchangeFood.getScore()){
            throw new MallException(MemberResultCode.INSUFFICIENT_SCORE);
        }
        member.setScore(member.getScore()-exchangeFood.getScore());
        updateById(member);
        ExchangeRecord exchangeRecord = new ExchangeRecord();
        exchangeRecord.setExchangeName(exchangeFood.getSkuName());
        exchangeRecord.setExchangeDate(new DateTime());
        exchangeRecord.setMemberId(memberId);
        exchangeRecord.setScore(exchangeFood.getScore());
        exchangeRecord.setState("FOOD");
        exchangeRecordService.save(exchangeRecord);
    }

    @Override
    public void exchangeCoupon(Integer memberId, Integer exchangeCouponId) {
        Member member = getById(memberId);
        if (member==null){
            throw new MallException(MemberResultCode.USER_DOES_NOT_EXIST);
        }
        ExchangeCoupon exchangeCoupon = exchangeCouponService.getById(exchangeCouponId);
        if (member.getScore()<exchangeCoupon.getScore()){
            throw new MallException(MemberResultCode.INSUFFICIENT_SCORE);
        }
        member.setScore(member.getScore()-exchangeCoupon.getScore());
        MemberCoupon memberCoupon = new MemberCoupon();
        memberCoupon.setMemberId(memberId);
        memberCoupon.setCouponId(exchangeCouponId);
        memberCoupon.setReceiveTime(new Date());
        Result<CouponDto> couponDtoResult = couponQueryClient.queryCouponById(exchangeCoupon.getCouponId());
        memberCoupon.setExpirationTime(couponDtoResult.getData().getCouponEndTime());
        memberCoupon.setUseState("UNUSE");
        memberCouponService.save(memberCoupon);
        updateById(member);
        ExchangeRecord exchangeRecord = new ExchangeRecord();
        exchangeRecord.setExchangeName(exchangeCoupon.getCouponName());
        exchangeRecord.setExchangeDate(new DateTime());
        exchangeRecord.setMemberId(memberId);
        exchangeRecord.setScore(exchangeCoupon.getScore());
        exchangeRecord.setState("COUPON");
        exchangeRecordService.save(exchangeRecord);
        couponManageClient.memberChangeCouponReceive(exchangeCoupon.getCouponId());

    }

    @Override
    public MemberDto loginTable(Integer tableId, String wechatNum, String openId, String phone,Integer diningPerson, String userName, Integer storeId, String memberImg) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wechat_num",wechatNum).eq("store_id",storeId);
        Member member = memberMapper.selectOne(queryWrapper);
        if (member!=null){
            AddTableUsedRecordParam addTableUsedRecordParam = new AddTableUsedRecordParam();
            addTableUsedRecordParam.setTableId(tableId);
            addTableUsedRecordParam.setStoreId(storeId);
            addTableUsedRecordParam.setUsedTime(new Date());
            addTableUsedRecordParam.setDiningPerson(diningPerson);
            addTableUsedRecordParam.setTableUsedState("ORDERING");
            tableUserRecordMannageClient.addTableUsedRecord(addTableUsedRecordParam);
            return BeanUtil.copyProperties(member,MemberDto.class);
        }
        QueryWrapper<Member> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("phone",phone).eq("store_id",storeId);
        Member member1 = memberMapper.selectOne(queryWrapper1);
        if (member1!=null){
            throw new MallException(MemberResultCode.DUPLICATE_PHONE_NUMBER);
        }
        Member addMember = new Member();
        addMember.setStoreId(storeId);
        addMember.setRoleId(1);
        addMember.setWechatNum(wechatNum);
        addMember.setPhone(phone);
        addMember.setUserName(userName);
        addMember.setMemberImg(memberImg);
        addMember.setOpenId(openId);
        save(addMember);
        QueryWrapper<Member> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wechat_num",wechatNum).eq("store_id",storeId);
        Member member2 = memberMapper.selectOne(queryWrapper2);
        AddTableUsedRecordParam addTableUsedRecordParam = new AddTableUsedRecordParam();
        addTableUsedRecordParam.setTableId(tableId);
        addTableUsedRecordParam.setStoreId(storeId);
        addTableUsedRecordParam.setUsedTime(new Date());
        addTableUsedRecordParam.setDiningPerson(diningPerson);
        addTableUsedRecordParam.setTableUsedState("ORDERING");
        tableUserRecordMannageClient.addTableUsedRecord(addTableUsedRecordParam);
        return BeanUtil.copyProperties(member2,MemberDto.class);
    }


}
